package jwlee.testlog;

import java.io.IOException;
import java.util.*;
import java.util.logging.*;
import java.util.logging.Formatter;

public class TestLogging {
    
    private static List logList = new ArrayList();

    public static void main(String[] args) {

        Logger log1 = Logger.getLogger("logger1");
        Logger log2 = Logger.getLogger("logger2");
        log1.setLevel(Level.ALL);
        log2.setLevel(Level.SEVERE);
        log1.info("log1");
        log2.severe("log2");

        log1.setUseParentHandlers(false);
        try {
            FileHandler fHandler = new FileHandler("TestLogging.xml");
            fHandler.setFormatter(new SimpleFormatter());
            log1.addHandler(fHandler);

            ConsoleHandler cHandler = new ConsoleHandler();
            // cHandler.setFormatter(new XMLFormatter());
            cHandler.setFormatter(new Formatter() {// 给Handler设置格式器
                        public String format(LogRecord record) {
                            return record.getLevel() + "-:-"
                                    + record.getSourceClassName() + "-:-"
                                    + record.getSourceMethodName() + "-:-"
                                    + record.getMessage();
                        }
                    });
            log1.addHandler(cHandler);

            log1.addHandler(new Handler() {
                public void publish(LogRecord record) {
                    //System.out.println("new handler.publish()");
                    logList.add(record.getLevel());
                    logList.add(record.getSourceClassName());
                    logList.add(record.getSourceMethodName());
                    logList.add(record.getMessage());
                }

                public void flush() {
                    //System.out.println("new handler.flush()");
                }

                public void close() throws SecurityException {
                    //System.out.println("new handler.close()");
                }
            });
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log1.info("log1 info");
        System.out.println("logList:" + logList);

        class A {
        }
        class B {
        }
        log2.log(Level.SEVERE, "A", new A());
        log2.log(Level.SEVERE, "B", new B());
        log2.setFilter(new Filter() {// 给Logger设置过滤器
                    public boolean isLoggable(LogRecord record) {
                        Object[] params = record.getParameters();
                        if (params == null)
                            return true;
                        if (params[0] instanceof A)
                            return true;
                        return false;
                    }
                });
        log2.severe("after setting filter...");
        log2.log(Level.SEVERE, "A", new A());
        log2.log(Level.SEVERE, "B", new B());
        
        Logger log3=Logger.getLogger("com");
        Logger log4=Logger.getLogger("com.mzj");
        log3.setLevel(Level.SEVERE);//"com"中都被改变，包括"com.mzj"
        log3.info("log3");
        log4.info("log4");
        log4.setLevel(Level.FINE);//"com.mzj"有了自己的级别
        log3.info("log3");
        log4.info("log4");
        log3.setLevel(Level.WARNING);//这次"com.mzj"不再受"com"影响
        log3.info("log3");
        log4.info("log4");
        
        Logger log5=Logger.getLogger("log5");
        log5.info("log5");
    }

}
